package com.itheima.controller.backend.order;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Api(tags = "检查项管理")
@Slf4j
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    /*
    * 新增检查项
    * */
    @PostMapping("/add")
    @ApiOperation(value = "新增检查项")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项:{}",checkItem);
        checkItemService.add(checkItem);
        return new Result(true, "新增成功" ,null);
    }

    /*
     * 根据id删除检查项
     * */
    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除检查项")
    public Result deleteById(Integer id) {
        log.info("根据id删除检查项:{}",id);
        checkItemService.deleteById(id);
        return new Result(true, "删除成功", null);
    }

    /*
     * 修改检查项
     * */
    @PostMapping("/edit")
    @ApiOperation(value = "修改检查项")
    public Result update(@RequestBody CheckItem checkItem) {
        log.info("修改检查项:{}",checkItem);
        checkItemService.update(checkItem);
        return new Result(true, "修改成功", null);
    }

    /*
     * 分页查询检查项
     * */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询检查项")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查项:{}",queryPageBean);
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        return pageResult;
    }

    /*
    * 根据id查询检查项
    * */
    @GetMapping("/findById")
    @ApiOperation(value = "根据id查询检查项")
    public Result findById(Integer id){
        log.info("根据id查询检查项:{}",id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, "查询成功" ,checkItem);
    }

    /*
     * 查询所有检查项
     * */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItems = checkItemService.findAll();
        return new Result(true, "查询成功" ,checkItems);
    }

    /*
     * 根据检查组id查询关联的检查项id
     * */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation(value = "根据检查组id查询关联的检查项id")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        log.info("根据检查组id查询关联的检查项id:{}",checkgroupId);
        List<Integer> checkItemIds = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
        return new Result(true, "查询检查项成功" ,checkItemIds);
    }
}
